using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LanguageInfo.Common.Utilities
{
	/// <summary>
	/// 描述：数据库说明书PDF文件和二进制流相互转化
	/// 作者：路晓阳
	/// 时间：2010-06-22 3:21:27 PM
	/// <summary>
	public class PdfHelper
	{
		#region PDF文件转化为二进制流
		/// <summary>
		/// PDF文件转化为二进制流
		/// </summary>
		/// <param name="strPDFFile">PDF文件名</param>
		/// <returns>二进制流</returns>
		public static byte[] PDFToByte(Stream stream)
		{
			try
			{
				//打开文件
				//FileStream fs = new FileStream(strPDFFile, FileMode.Open, FileAccess.Read);				
				Byte[] b = new Byte[stream.Length];
				stream.Read(b, 0, b.Length);

				for (int n = 0; n < b.Length; n++)
				{
					b[n] = (byte)(b[n] ^ 27);  //与 27 异或加密
				}

				stream.Close();
//				streamfs = null;
				return b;
			}
			catch
			{
				GC.Collect();

				//logg
				throw;
			}
		}
		#endregion


		#region 读取数据库的二进制文件到PDF文件中
		/// <summary>
		/// 解密PDF流
		/// </summary>
		/// <param name="_BinaryStreamData">从数据库中读取的二进制流数据</param>
		public static byte[] ByteToPDF(byte[] _BinaryStreamData)
		{
			try
			{
				if (_BinaryStreamData != null)
				{
					byte[] imgData = new byte[_BinaryStreamData.Length];
					//imgData = _BinaryStreamData;

					for (int n = 0; n < _BinaryStreamData.Length; n++)
					{
						imgData[n] = (byte)(_BinaryStreamData[n] ^ 27); //解密
					}

					return imgData;
				}
				else
				{
					return null;
				}
			}
			catch
			{
				GC.Collect();

				//logging
				throw ;
			}
		}

		#endregion
	}
}
